package druid;

import JDBCUtil.JDBCUtil;
import com.alibaba.druid.sql.visitor.functions.Char;
import entity.User;
import entity.User2;
import sun.rmi.server.UnicastServerRef2;

import java.io.*;
import java.net.UnknownServiceException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;

/**
 * 十万条数据 与数据库十万条数据匹配
 */
public class Test2 {
    public static void main(String[] args) {
        try {
            long start = System.currentTimeMillis();
            long end = System.currentTimeMillis();
            List<User> dbUsers =readForDB();
            System.out.println(1);
            List<User> ioUsers = readForIO();
            System.out.println(2);
            print(dbUsers,ioUsers);
            System.out.println((end - start) / 1000 );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static  List<User> readForIO() throws Exception {
        FileInputStream fileInputStream = new FileInputStream("user.txt");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        InputStreamReader tsr = new InputStreamReader(bufferedInputStream);
        Map<Integer, User> map = new TreeMap<>();
        List<User> users = new ArrayList<>();
        BufferedReader br = new BufferedReader(tsr,1024 * 1024);
        String line ;
        int id ;
        String[] arr ;
        while ((line = br.readLine()) != null ){
            User user = new User();
            arr = line.split(",");
            id = Integer.parseInt(arr[0]);
            user.setId(id);
            user.setPhone(arr[1]);
            user.setCity(arr[2]);
            map.put(id,user);
        }
        br.close();
        for ( Map.Entry<Integer,User> entry: map.entrySet()
             ) {
            User u = entry.getValue();
            users.add(u);
        }


        return users;
    }
    public static List<User> readForDB(){
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = "select id,name,age,sex from user";
            List<User> users = new ArrayList<>();
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            while(resultSet.next()){
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String sex = resultSet.getString("sex");
                User user = new User();
                user.setId(id);
                user.setName(name);
                user.setSex(sex);
                user.setAge(age);
                users.add(user);
            }
            return users;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(conn,null,null);
        }
        return null;
    }

    public static void print(List<User> users1,List<User> users2){
        List<User> addUser = new ArrayList<>();
        int len1 = users1.size();
        int len2 = users2.size();
        int p1 =0,p2 =0;
        for (;;){
            Integer a1 = users1.get(p1).getId();
            Integer a2 = users2.get(p2).getId();
            if (a1.equals(a2)){
                User user = users1.get(p1);
                User user1 = users2.get(p1);
                User user2 = new User();
                user2.setId(user.getId());
                user2.setName(user.getName());
                user2.setAge(user.getAge());
                user2.setSex(user.getSex());
                user2.setPhone(user1.getPhone());
                user2.setCity(user1.getCity());
                addUser.add(user);
            } else {
                if (a1 > a2){
                    p2 ++;
                }else {
                    p1++;
                }
            }
            if (p1 >= len1 || p2 > len2){
                break;
            }
        }
        for ( int i =0 ; i < addUser.size(); i++){
            System.out.println(addUser.get(i).getId());
        }
    }

}
